<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改</title>
    <link rel="stylesheet" href="/js/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/common.css" media="all">
</head>
<style>
    .layui-form-item{
        width: 50%;
    }
    label{
        width:95px !important;
    }
    .layui-input-block{
        margin-left:125px;
    }
</style>
<body>
<div style="width:80%;margin:30px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="imgid" name="imgid">

        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">轮播图片</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="image" id="image" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="mjimg" name="imgsrc">
            <input type="hidden" class="input-text" id="topicid" name="topicid">
            <input type="hidden" class="input-text" id="type" name="type">
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button style="width: 100px;" class="layui-btn" id="updCountry" lay-submit lay-filter="updCountry">确定</button>
                <button style="width: 100px;" type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/js/layui/layui.all.js"></script>
<script>
    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(decodeURI(window.location.search));
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }
    // var image = "" ;
    var imgid = GetQueryString("id");
    layui.use(['form' , 'jquery' , 'upload',], function() {
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        $.ajax({
            url:"/lunbo/getone",
            data:{id:imgid},
            success:function (res) {
                if(res.code==200) {
                    $("#image").attr("src", '/upload/' + res.data.imgsrc);
                    $("#type").val(res.data.type);
                    $("#topicid").val(res.data.topicid);
                    $("#imgid").val(res.data.imgid);
                }
            }
        })

        //添加操作
        form.on('submit(updCountry)',function(data){
            // console.log(data)
            // data.field.topicid=$("#topicid").val();
            // data.field.type=$("#type").val();
            // data.field.imgid=$("#imgid").val();
            if (data.field.imgsrc =="" || data.field.imgsrc == null){
                layer.msg("请上传新图片",{icon:2,time:2000});
                return false;
            }
            // if(image == "") {
            //     layer.msg("请先上传图片")
            //     return
            // }
            // data.field.mjimg  = image
            layer.load();
            $.ajax({
                url: "/lunbo/update"
                , type: "POST"
                , contentType: 'application/json'
                , data: JSON.stringify(data.field)
                ,success:function (data) {
                    if(data.code==200){
                        layer.msg('修改成功');
                        setTimeout(function(){
                            parent.layer.closeAll();
                            window.location.reload();
                        },1500);
                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }
                }
                ,error:function () {
                    console.log("ajax error");
                }
            });
            layer.closeAll();
            return false;
        });


        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: '/upload' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#image').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    // image = res.data
                    $("#mjimg").val(res.data);
                    return layer.msg('上传成功！');
                } else {
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });

    })
</script>

</body>
</html>